package com.jackiechancinema.ssm.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.OfficialBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.OfficialDao;
import com.jackiechancinema.ssm.entity.Drills;
import com.jackiechancinema.ssm.entity.Official;
import com.jackiechancinema.ssm.service.Inte.OfficialService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class OfficialServiceImpl implements OfficialService {
    @Autowired
    private OfficialDao officialDao;

    @Override
    public PageInfo<Official> findAll(int page, int limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Official> pageInfo = new PageInfo<Official>(officialDao.queryAll());
        return pageInfo;
    }

    @Override
    public R OffAdd(Official official) {
        int i = officialDao.insertAdd(official);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R OffUpdate(Official official) {
        int i = officialDao.updateUpdate(official);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R OffDel(int id) {
        int i = officialDao.deleteDel(id);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public PageVo<Official> findAllV2(OfficialBo bo) {
        if (bo != null){
            //组装查询条件
            Official official = new Official();
            if (StringUtils.hasLength(bo.getName())){
                official.setName(bo.getName());
            }
            if (StringUtils.hasLength(bo.getPost())){
                official.setPost(bo.getPost());
            }

            PageHelper.startPage(bo.getPage(),bo.getLimit());
            PageInfo<Official> pageInfo = new PageInfo<>(officialDao.queryAllV2(official));
            return new PageVo<Official>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());

        }
        return new PageVo<Official>(SystemContant.PAGE_R,"OK",0 ,null);
    }
}
